﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using GroundWater.Data;
using GroundWater.BusinessActions;
using WellTable = GroundWater.Data.InfoDataSet.WellsDataTable;
using WellRow = GroundWater.Data.InfoDataSet.WellsRow;
using System.Web.Script.Serialization;


namespace GroundWater.Basedata
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WellHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World"); //application/json;utf-8
            string op = context.Request.QueryString["op"];
            if (op == "fetch")
            {
                string wellCode = context.Request.Form["wellcode"];
                WellRow row = Well.GetById(wellCode, false);

                var well = new WellInfo
                {
                    WellCode = row.Code,
                    WellName = row.Name,
                    Longitude = row.Longitude,
                    Latitude = row.Latitude,
                    Depth = row.Depth,
                    ShaftSinkingDate = row.ShaftSinkingDate,
                    SIMCard = row.SIMCard,
                    BTUAddress = row.BTUAddress,
                    SectionPlane = row.SectionPlane
                };

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(well);
                context.Response.ContentType = "application/json";
                context.Response.Write(json);
                return;
            }

            if (op == "update" || op == "insert")
            {
                string well = context.Request.Form["well"];
                JavaScriptSerializer s = new JavaScriptSerializer();
                WellInfo wellInfo = s.Deserialize<WellInfo>(well);

                WellRow dataRow = null;
                if (op == "insert")
                {
                    WellTable wells = new GroundWater.Data.InfoDataSet().Wells;
                    dataRow = wells.NewWellsRow();
                    dataRow.Code = "";
                    dataRow.Name = "";
                    dataRow.WaterSource = "dd";
                    dataRow.ResourceRegionCode = "dd";
                    dataRow.AdministrativeDivisionsCode = "dd";
                    dataRow.FunctionRegionCode = "ss";
                    dataRow.DesignYearlyWaterQuantity = 0m;
                    wells.AddWellsRow(dataRow);
                }
                else
                {
                    dataRow = Well.GetById(wellInfo.WellCode, false);
                }

                EditWell(wellInfo, dataRow);

                Well.Update(dataRow.Table.DataSet, false, true, new string[] { dataRow.Table.TableName });

                return;
            }
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void EditWell(WellInfo well, WellRow row)
        {
            row.Code = well.WellCode;
            row.Name = well.WellName;
            row.Longitude = well.Longitude;
            row.Latitude = well.Latitude;
            row.Depth = well.Depth;
            row.ShaftSinkingDate = well.ShaftSinkingDate;
            row.SIMCard = well.SIMCard;
            row.BTUAddress = well.BTUAddress;
            row.SectionPlane = well.SectionPlane;
        }
    }

    [Serializable]
    public class WellInfo
    {
        public string WellCode { get; set; }
        public string WellName { get; set; }
        public decimal Longitude { get; set; }
        public decimal Latitude { get; set; }
        public decimal Depth { get; set; }
        public DateTime ShaftSinkingDate { get; set; }
        public string SIMCard { get; set; }
        public string BTUAddress { get; set; }
        public string SectionPlane { get; set; }
    }

}
